package com.gotloop.controllers;

import javax.naming.NameNotFoundException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.gotloop.services.LoopService;
import com.gotloop.util.WebConstants;

/**
 * Mapping every Loop related requests.
 * @author jibhaine
 *
 */
@Controller
public class LoopController {

	/**
	 * Logger for LoopController.
	 */
	private static final Logger LOG = LoggerFactory.getLogger(LoopController.class);
	
	/**
	 * Loop Service.
	 */
	private LoopService loopService;
	
	/**
	 * Loop or loop home page.
	 * @return loopList view and a list of last loops.
	 */
	@RequestMapping(value = {"/loops", "/loop" })
	public ModelAndView showLastLoops() {
		LOG.debug("showLastLoops");
		loopService.findLastLoops(0, WebConstants.LOOPS_PER_PAGE);
    	return new ModelAndView("loopList");
    }

	/**
	 * Loop detail page (by Id).
	 * @param idLoop the primary key of the loop.
	 * @return loopDetail view and the requested loop.
	 */
    @RequestMapping(value = "/loop/{id}", method = RequestMethod.GET)
	public ModelAndView showLoopById(@PathVariable Long idLoop) {
		LOG.debug("showLoopById | id : {}", idLoop);
    	return new ModelAndView("loopDetails");
    }
	/**
	 * Loop detail page (by UrlTitle).
	 * @param title the title of the loop in string format.
	 * @return loopDetail view and the requested loop.
	 */
    @RequestMapping(value = "/loop/{title}", method = RequestMethod.GET)
	public ModelAndView showLoopByTitle(@PathVariable String title) {
		LOG.debug("showLoopByTitle | title : " + title);
    	return new ModelAndView("loopDetails");
    }
    
	/**
	 * Loop Edit.
	 * @param idLoop the primary key of the loop.
	 * @return loopList view and a list of last loops.
	 * @throws NameNotFoundException hey
	 */
    @RequestMapping(value = "/loop/{id}/edit", method = RequestMethod.GET)
	public ModelAndView editLoopDetails(@PathVariable Long idLoop){
		LOG.debug("editLoopDetails | id : {}", idLoop);
    	if (LOG.isDebugEnabled()) {
    	}
    	return new ModelAndView("loopDetails");
    }
    
	/**
	 * Save Loop modification function.
	 * @param idLoop the primary key of the loop.
	 * @return loopList view and a list of last loops.
	 */
    @RequestMapping(value = "/loop/${id}/edit", method = RequestMethod.PUT)
	public ModelAndView modifyLoopDetail(@PathVariable Long idLoop) {
		LOG.debug("modifyLoopDetail | id : {}", idLoop);
    	/*
    	 * if update failed
    	 *   	return new ModelAndView("loopEdit");
    	 */
    	return new ModelAndView("loopDetails");
    }

	/**
	 * Save Loop modification function.
	 * @param idLoop the primary key of the loop.
	 * @return loopList view and a list of last loops.
	 */
    @RequestMapping(value = "/loop/${id}/edit", method = RequestMethod.DELETE)
	public ModelAndView deleteLoop(@PathVariable Long idLoop) {
		LOG.debug("deleteLoop | id : {}", idLoop);
    	/**
    	 * only Loop.author = User authenticated.
    	 * if delete failed
    	 *   	return new ModelAndView("loopEdit");
    	 */
    	return new ModelAndView("loopList");
    }
}
